package com.ai.pos.client.sales.view;


import com.ai.pos.client.SalesItemService;
import com.ai.pos.client.SalesItemServiceAsync;
import com.ai.pos.client.sales.models.SalesItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;

public class NewSalesItem extends Composite implements HasText, SalesItemServiceAsync {

	private static NewSalesItemUiBinder uiBinder = GWT
			.create(NewSalesItemUiBinder.class);

	private final SalesItemServiceAsync NewItemService = GWT
			.create(SalesItemService.class);
	
	interface NewSalesItemUiBinder extends UiBinder<Widget, NewSalesItem> {
	}

	public NewSalesItem() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	Button button;

	public NewSalesItem(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
		button.setText(firstName);
	}

	@UiHandler("button")
	void onClick(ClickEvent e) {
		
		SalesItem item = new SalesItem();
		item.SetLongName("TestItem");
		item.setButtonOneName("but One");
		item.setButtonTwoName("buttwo");
		
		//String newItemJson = item.getJson();
		
		SaveItem(item, new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String result) {
				Window.alert(result);
				
			}
			
		});
	}

	public void setText(String text) {
		button.setText(text);
	}

	public String getText() {
		return button.getText();
	}


	@Override
	public void getItem(int itemId, AsyncCallback<String> callback) throws IllegalArgumentException {
		NewItemService.getItem(itemId, callback);
		
	}

	@Override
	public void getAllItems(String itemJson, AsyncCallback<String> callback) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void SaveItem(SalesItem itemJson, AsyncCallback<String> callback) throws IllegalArgumentException {
		NewItemService.SaveItem(itemJson, callback);		
	}

}
